package com.wwj.netty.bio.data;

import java.io.*;
import java.net.Socket;

/**
 * Created by lenovo on 2018/11/12.
 */
public class ServerHandler implements Runnable{

    private Socket socket;
    public ServerHandler(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        DataInputStream in=null;
        DataOutputStream out=null;

        try {
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());

            String message = in.readUTF();
            System.out.println("服务器收：" + message);

            System.out.println("请输入：");
            String data= new BufferedReader(new InputStreamReader(System.in)).readLine();
            out.writeUTF(data);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
